package com.springcloud.sc.ucpv5.client.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateToolUtil {
	private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
	private static SimpleDateFormat formatter_HHmmss = new SimpleDateFormat("HH:mm:ss"); //$NON-NLS-1$
	private static SimpleDateFormat formatter_MMddHHmmss = new SimpleDateFormat("MM-dd HH:mm:ss"); //$NON-NLS-1$
	private static SimpleDateFormat formatter_yyyyMMdd = new SimpleDateFormat("yyyyMMdd"); //$NON-NLS-1$
	private static SimpleDateFormat formatter_yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat formatter_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");

	public static String getCurData() {
		String curDate = null;
		try {
			curDate = formatter_yyyy_MM_dd.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	public static String getCurData_yyyyMMdd() {
		String curDate = null;
		try {
			curDate = formatter_yyyyMMdd.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	public static String getCurTime() {
		String curDate = null;
		try {
			curDate = formatter_HHmmss.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	public static long getCurTime_Long() {
		return (new Date()).getTime();
	}

	public static long getCurTime_Long_1970() {
		Date standardDate = new Date(0);
		return (new Date()).getTime() - standardDate.getTime();
	}

	public static String getCurTimeFileName() {
		String curDate = null;
		try {
			curDate = formatter_yyyyMMddHHmmss.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	/**
	 * 取得当前日期时间的字符串类型
	 * 
	 * @return
	 */
	public static String getCurDataTime() {
		String curDate = null;
		try {
			curDate = formatter.format(new Date());
		} catch (Exception e) {
		}

		return curDate;
	}

	/**
	 * 取得当前日期时间的字符串类型 且在当前琪琪基础上, 改变小时
	 * 
	 * @return
	 */
	public static String getCurDataTime_Hour(int hour) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR, hour);

		Date curDate = null;
		try {
			curDate = formatter.parse(formatter.format(cal.getTime()));
		} catch (ParseException e) {

		}

		String curDate_String = null;
		try {
			curDate_String = formatter.format(curDate);
		} catch (Exception e) {
		}

		return curDate_String;
	}

	/**
	 * 从日期转为字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromDate(Date date) {
		try {
			String c = formatter.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";

	}

	/**
	 * 从日期转为字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromDate_yyyyMMddHHmmss(Date date) {
		try {
			String c = formatter_yyyyMMddHHmmss.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";

	}

	/**
	 * 从日期转为字符串
	 * 
	 * @param time
	 * @return
	 */

	public static String getStringFromDate(String dateFormat, String date_String) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

		Date date = null;
		try {
			date = sdf.parse(date_String);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return getStringFromDate(date);
	}

	/**
	 * 取得简单日期显示
	 * 
	 * @param date_String
	 * @return
	 */
	public static String getShortStringFromDate(String date_String) {
		if (date_String == null || "".equals(date_String)) {
			return "";
		}
		try {
			return getStringFromTime("dd HH:mm:ss", getDateFromString(date_String));
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}

	/**
	 * 取得简单日期显示
	 * 
	 * @param date_String
	 * @return
	 */
	public static String getShortStringFromDate_dd_HHMM(String date_String) {
		if (date_String == null || "".equals(date_String)) {
			return "";
		}
		try {
			return getStringFromTime("dd HH:mm", getDateFromString(date_String));
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}

	/**
	 * 取得简单日期显示
	 * 
	 * @param date_String
	 * @return
	 */
	public static String getShortStringFromDate_HHMM(String date_String) {
		if (date_String == null || "".equals(date_String)) {
			return "";
		}
		try {
			return getStringFromTime("HH:mm", getDateFromString(date_String));
		} catch (Exception e) {
			// TODO: handle exception
		}

		return "";
	}

	/**
	 * 从日期转为时间字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getStringFromTime(String dateFormat, Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		try {
			String c = sdf.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";
	}

	/**
	 * 从日期转为时间字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String getStringFromTime(Date date) {
		try {
			String c = formatter_HHmmss.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";
	}

	/**
	 * 从日期对象转为日期字符串
	 * 
	 * @param date
	 * @return
	 */

	public static String getDateStringFromTime(Date date) {
		try {
			String c = formatter_yyyy_MM_dd.format(date);
			return c;

		} catch (Exception e) {
			// TODO: handle exception
			// e.printStackTrace();
		}

		return "";
	}

	/**
	 * 从字符串转为日期
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getDateFromString(String dateString) {
		Date date = null;
		try {
			date = formatter.parse(dateString.trim());
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 从字符串转为日期
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getDateFromString_yyyyMMdd(String dateString) {
		Date date = null;
		try {
			date = formatter_yyyy_MM_dd.parse(dateString);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 从字符串转为时间
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date getTimeFromString(String timeString) {
		Date date = null;
		try {
			date = formatter_HHmmss.parse(timeString);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return date;
	}

	private static Date standardDate = new Date(0);// PublicUtil.stringToDate("1970-01-01","yyyy-mm-dd");

	/**
	 * 以1970-1-1的时间为基准,转换int值为时间
	 * 
	 * @date 2006-7-11
	 * @param time
	 * @return
	 * @see
	 */
	public static Date intToDate(int time) {
		if (time < 0) {
			return new Date(standardDate.getTime());
		} else {
			long tmp = time;
			tmp = tmp * 1000;
			tmp += standardDate.getTime();

			return new Date(tmp);
		}
	}

	/**
	 * 以1970-1-1的时间为基准,转换时间值为int
	 * 
	 * @date 2006-7-11
	 * @param date
	 * @return
	 * @see
	 */
	public static int dateToInt(Date date) {
		long minTime = standardDate.getTime();
		long curTime = date.getTime();
		long ret = (curTime - minTime) / 1000;
		if (ret < 0) {
			return 0;
		} else if (ret > Integer.MAX_VALUE) {
			return Integer.MAX_VALUE;
		} else {
			return (int) ret;
		}
	}

	/**
	 * 以1970-1-1的时间为基准,转换时间值为int
	 * 
	 * @param time
	 * @return
	 */
	public static String getTimeString(long time) {
		try {
			Date date = intToDate((int) time);
			return formatter.format(date);
		} catch (Exception e) {
		}

		return ""; //$NON-NLS-1$
	}

	public static String getTimeShortString(long time) {
		try {
			Date date = intToDate((int) time);
			return formatter_HHmmss.format(date);
		} catch (Exception e) {
		}

		return ""; //$NON-NLS-1$
	}

	public static String getTimeYearMouDayShortString(long time) {
		try {
			Date date = intToDate((int) time);
			return formatter_yyyyMMdd.format(date);
		} catch (Exception e) {
		}

		return ""; //$NON-NLS-1$
	}

	public static String getTimeDayShortString(long time) {
		try {
			Date date = intToDate((int) time);
			return formatter_MMddHHmmss.format(date);
		} catch (Exception e) {
		}

		return ""; //$NON-NLS-1$
	}

	/**
	 * 获取从1970到现在的时间, 主要是适配gps
	 * 
	 * @param dateString
	 * @return
	 */
	public static long getTime_Long_1970(String dateString) {
		Date date = null;
		try {
			date = formatter.parse(dateString);
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		Date standardDate = new Date(0);
		return (date.getTime() - standardDate.getTime()) / 1000;
	}
}
